package com.example.myapplication.dao;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;

import java.net.URL;


public class TxtDao {
    public int number;
    public boolean ischanged = false;


    public int getTxt(String id, Context context,Handler handler) {
        new Thread(() -> {
            HttpURLConnection conn;
            try {
                Context ct = context;
                URL url = new URL(HttpDao.urladd + "GetTxt");
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                //上传参数 实现post
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(conn.getOutputStream());
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                bufferedWriter.write("id=" + id);
                bufferedWriter.flush();
                outputStreamWriter.close();
                bufferedWriter.close();
                //获得数据传入flag 并改变ischanged
                DataInputStream inputStream = new DataInputStream(conn.getInputStream());

                int available = inputStream.readInt();
                // System.out.println(conn.getInputStream().available());
                // byte[] bytes = new byte[available];
                //conn.getInputStream().read(bytes);
                //InputStreamReader isr = new InputStreamReader(conn.getInputStream(), "gbk");// 字符流
                //BufferedReader br = new BufferedReader(isr);// 缓冲流
               // Log.d("233",  + Environment.getExternalStorageDirectory());
                FileOutputStream outputStream = ct.openFileOutput(id + ".txt", Context.MODE_PRIVATE);
                InputStream inputStream1=conn.getInputStream();
               // OutputStreamWriter outputStreamWriter1 =new OutputStreamWriter(outputStream,"gbk");
                byte[] bytes = new byte[1024];
                int len = -1;
                while ((len = inputStream1.read(bytes)) != -1&& available > len) {
                    outputStream.write(bytes,0,len);   available = available - len;
                }

                /*String str = null;
                while ((str = br.readLine()) != null) {
                   outputStreamWriter1.write(str);
                }*/


                outputStream.flush();
                inputStream1.close();
                outputStream.close();

                Message message= handler.obtainMessage();
                message.what=3;
                handler.sendMessage(message);

                /*br.close();
                isr.close();*/

            } catch (IOException e) {
                e.printStackTrace();

            }
        }).start();
        return number;
    }


}
